import {
	IParam,
	IPickUpListViewAbility,
	IPickupTreeViewAbility,
	IPickupViewPanelAbility,
	IPickupViewPanelController,
	IPickupViewPanelControllerParams,
	IPickupViewPanelModel,
	IPickupViewPanelStore,
	PickupViewPanelActionType,
} from '@/core/interface';
import { DECtrlController } from './de-ctrl-controller';

/**
 * 选择视图面板部件控制器
 *
 * @export
 * @class PickupViewPanelController
 * @extends {DECtrlController<PickupViewActionType, IPickupViewPanelStore, IPickupViewPanelAbility>}
 */
export class PickupViewPanelController
	extends DECtrlController<
		PickupViewPanelActionType,
		IPickupViewPanelStore,
		IPickupViewPanelAbility
	>
	implements
		IPickupViewPanelController<IPickupViewPanelStore, IPickupViewPanelAbility>
{
	/**
	 * 选择视图面板模型
	 *
	 * @type {IPickupViewPanelModel}
	 * @memberof PickupViewPanelController
	 */
	declare model: IPickupViewPanelModel;

	/**
	 * Creates an instance of PickupViewPanelController.
	 * @param {IPickupViewPanelControllerParams<PickupViewPanelActionType, IPickupViewPanelAbility>} params
	 * @memberof PickupViewPanelController
	 */
	public constructor(
		params: IPickupViewPanelControllerParams<
			PickupViewPanelActionType,
			IPickupViewPanelAbility
		>
	) {
		super(params);
		this.ctrlInit(params);
	}

	/**
	 * 获取嵌入视图能力
	 *
	 * @return {*}  {(IPickUpListViewAbility | IPickupTreeViewAbility | undefined)}
	 * @memberof PickupViewPanelController
	 */
	public getEmbedViewAbility():
		| IPickUpListViewAbility
		| IPickupTreeViewAbility
		| undefined {
		if (this.model.embedViewCodeName) {
			return this.getSubAbility<
				IPickUpListViewAbility | IPickupTreeViewAbility
			>(this.model.embedViewCodeName);
		}
		return undefined;
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	public handleCtrlAction(name: string, action: string, data: IParam[]): void {
		super.handleCtrlAction(name, action, data);
		if (this.model.embedViewCodeName === name) {
			this.handleEmbedViewAction(action, data);
		}
	}

	/**
	 * 处理嵌入视图事件
	 *
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	public handleEmbedViewAction(action: string, data: IParam[]) {
		switch (action) {
			case 'viewDataChange':
				this.handleValueChange(data);
				break;
			case 'viewDataActive':
				this.handleDataActive(data);
				break;
			case 'viewClose':
				this.closeView ? this.closeView(data) : null;
				break;
		}
	}

	/**
	 * 处理数据变化
	 *
	 * @private
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	private handleValueChange(data: IParam[]) {
		this.emit('selectionChange', data);
	}

	/**
	 * 处理数据激活
	 *
	 * @private
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	private handleDataActive(data: IParam[]) {
		this.emit('dataActive', data);
	}

	/**
	 * 获取能力
	 *
	 * @return {*}  {IPickupViewPanelAbility}
	 * @memberof PickupViewPanelController
	 */
	getAbility(): IPickupViewPanelAbility {
		return {
			...super.getAbility(),
		};
	}
}
